﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using UniMana.Utils.Configs;

namespace UniMana.Win.Leadership
{
    public partial class Frm_AcademicHandling_Transfer_Course_Censorship : DevExpress.XtraEditors.XtraForm
    {
        private DataTable m_tblTable = new DataTable();
        private DBMain m_data = new DBMain();

        int m_iLog_TranferKey = 0;
        private Guid m_gStudentKey;
        private string IDbefore = "";
        private string IDafter = "";
        private int ClassBefore = 0;
        private int ClassAfter = 0;
        private int m_iProgramBefore = 0;
        private int m_iProgramAfter = 0;
        private int m_iType = 0;
        private Guid m_gRequireUser;
        private DateTime m_dTranferTime;
        private object m_oReasons;
        private object m_oFeedback;
        private bool m_bPermitValue;
        private bool m_bRefuseValue;
        private bool m_bStatusValue;

        public Frm_AcademicHandling_Transfer_Course_Censorship()
        {
            if (!SysUtils.CheckAccessByRule(RuleConfig.XLQCHV_CNCKCN))
            {
                return;
            }
            InitializeComponent();
        }

        private void Frm_AcademicHandling_Transfer_Course_Censorship_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// Lấy dữ liệu
        /// </summary>

        private void LoadData()
        {
            m_tblTable = new DataTable();
            m_tblTable = (this.m_data.m_DCReadOnly.Log_Tranfers.GetAllData()).ToDataTable();
            gctView.DataSource = m_tblTable;

            m_tblTable = new DataTable();
            m_tblTable = m_data.m_DCReadOnly.Training_TrainingPrograms.GetData().ToDataTable();
            lueTraining_TrainingProgram.GetData(m_tblTable, "Training_TrainingProgramKey", "Name");
            lueTraining_TrainingProgramTransfer.GetData(m_tblTable, "Training_TrainingProgramKey", "Name");
            lueTraining_Class.GetData(m_data.m_DCReadOnly.Training_Classes.GetKeyNameClass(), "Training_ClassKey", "Name");
            lueTraining_ClassNB.GetData(m_data.m_DCReadOnly.Training_Classes.GetKeyNameClass(), "Training_ClassKey", "Name");

            m_tblTable = new DataTable();
            m_tblTable = m_data.m_DCReadOnly.Personnel_Personnels.GetPersonnelKeyIDLastNameName().ToDataTable();
            if (m_tblTable.Rows.Count != 0)
                for (int i = 0; i < m_tblTable.Rows.Count; i++)
                    m_tblTable.Rows[i]["Name"] = m_tblTable.Rows[i]["LastName"] + " " + m_tblTable.Rows[i]["Name"];
            luePersonnel_Personnel.GetData(m_tblTable, "Personnel_PersonnelKey", "Name");

            rluTraining_Student.GetData(m_data.m_DCReadOnly.Training_Students.GetData(), "Training_StudentKey", "Name");
        }

        /// <summary>
        /// Đóng form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = XtraMessageBox.Show("Bạn muốn tắt duyệt phiếu yêu cầu ?", "Thông báo", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
                this.Close();
        }

        /// <summary>
        /// Chọn phiếu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gctView_Click(object sender, EventArgs e)
        {
            if (!grvView.IsFilterRow(grvView.FocusedRowHandle))
            {
                try
                {
                    m_iLog_TranferKey = int.Parse(this.grvView.GetRowCellValue(this.grvView.FocusedRowHandle, "Log_TranferKey").ToString());
                    ShowInformation(m_iLog_TranferKey);
                }
                catch { }
            } 
        }

        /// <summary>
        /// Lấy các giá trị tương ứng với mã phiếu
        /// </summary>
        /// <param name="m_iLog_TranferKey"></param>

        private void GetValues(int m_iLog_TranferKey)
        {
            Log_TranferInfo tranferinfo = new Log_TranferInfo(m_iLog_TranferKey);
            if (tranferinfo.Properties.Log_TranferKey > 0)
            {
                m_gStudentKey = Guid.Parse(tranferinfo.Properties.Training_StudentKey.ToString());
                IDbefore = tranferinfo.Properties.BeforeID.ToString();
                IDafter = tranferinfo.Properties.AfterID.ToString();
                ClassBefore = int.Parse(tranferinfo.Properties.ClassBefore.ToString());
                ClassAfter = int.Parse(tranferinfo.Properties.ClassAfter.ToString());
                m_iProgramBefore = int.Parse(tranferinfo.Properties.Training_TrainingProgramKeyBefore.ToString());
                m_iProgramAfter = int.Parse(tranferinfo.Properties.Training_TrainingProgramKeyAfter.ToString());
                m_iType = int.Parse(tranferinfo.Properties.Training_TranferTypeKey.ToString());
                try { m_gRequireUser = Guid.Parse(m_data.m_DCReadOnly.System_Users.ReturnPersonnalWithUserKey(tranferinfo.Properties.RequireUser.ToString())); }
                catch { m_gRequireUser = Guid.Parse(DefaultConfig.EMPTY_GUID); }
                m_dTranferTime = Convert.ToDateTime(tranferinfo.Properties.TranferTime);
                m_oReasons = tranferinfo.Properties.Reasons;
                m_oFeedback = tranferinfo.Properties.Feedback;
                m_bStatusValue = (bool)(tranferinfo.Properties.Status);
            }
        }

        /// <summary>
        /// Hiển thị thông tin tương ứng với mã phiếu
        /// </summary>
        /// <param name="m_iLog_TranferKey"></param>

        private void ShowInformation(int m_iLog_TranferKey)
        {
            if (m_iLog_TranferKey > 0)
            {
                GetValues(m_iLog_TranferKey);
                lueTraining_TrainingProgram.EditValue = m_iProgramBefore;
                lueTraining_TrainingProgramTransfer.EditValue = m_iProgramAfter;
                if (m_iType == 1)
                    txtType.Text = "Chuyển khóa";
                else
                    if (m_iType == 2)
                        txtType.Text = "Chuyển ngành";
                    else
                        if (m_iType == 3)
                            txtType.Text = "Chuyển khóa và chuyển ngành";
                        else
                            txtType.Text = "";
                luePersonnel_Personnel.EditValue = m_gRequireUser;
                lueTraining_Class.EditValue = int.Parse(ClassBefore.ToString());
                lueTraining_ClassNB.EditValue = int.Parse(ClassAfter.ToString());
                txtDatetime.Text = m_dTranferTime.Day + "-" + m_dTranferTime.Month + "-" + m_dTranferTime.Year + " " + m_dTranferTime.Hour + ":" + m_dTranferTime.Minute + ":" + m_dTranferTime.Second;

                if (m_oFeedback != null)
                    rtbFeedback.Text = m_oFeedback.ToString();
                else
                    rtbFeedback.Text = "";
                if (m_bStatusValue)
                    rtbFeedback.Enabled = false;
                else
                    rtbFeedback.Enabled = true;
                if (m_oReasons != null)
                    rtbReason.Text = m_oReasons.ToString();
            }
        }

        /// <summary>
        /// Tô màu các mã phiếu chưa duyệt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void grvView_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (!View.IsFilterRow(e.RowHandle))
            {
                bool permit = Convert.ToBoolean(View.GetRowCellValue(e.RowHandle, View.Columns["Permit"]));
                bool refuse = Convert.ToBoolean(View.GetRowCellValue(e.RowHandle, View.Columns["Refuse"]));
                if ((permit == false) && (refuse == false))
                    e.Appearance.BackColor = Color.Silver;
            }
        }

        /// <summary>
        /// Các thao tác xử lý duyệt và không duyệt phiếu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void grvView_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (this.grvView.FocusedColumn == gclPermit)
            {
                m_iLog_TranferKey = int.Parse(this.grvView.GetRowCellValue(this.grvView.FocusedRowHandle, "Log_TranferKey").ToString());
                ShowInformation(m_iLog_TranferKey);
                Log_TranferInfo tranferinfo = new Log_TranferInfo(m_iLog_TranferKey);
                if (tranferinfo.Properties.Log_TranferKey > 0)
                {
                    GetValues(m_iLog_TranferKey);
                    if (tranferinfo.Properties.Status.Value == false)
                    {
                        DialogResult result = XtraMessageBox.Show("Bạn muốn phê duyệt phiếu " + m_iLog_TranferKey, "Thông báo", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            m_bPermitValue = (bool)this.grvView.GetFocusedRowCellValue(gclPermit);
                            this.grvView.SetFocusedRowCellValue(gclPermit, !m_bPermitValue);

                            // Thêm chương trình đào tạo mới cho sinh viên
                            Training_TrainingProgramInfo programinfo = new Training_TrainingProgramInfo(m_iProgramAfter);
                            Training_Student_ClassInfo trnclsinfo = new Training_Student_ClassInfo(m_gStudentKey.ToString(), m_iProgramAfter, true);
                            if (programinfo.Properties.Training_TrainingProgramKey > 0)
                            {
                                trnclsinfo.Properties.Mainly = true;
                                trnclsinfo.Save();
                            }
                            else
                            {
                                trnclsinfo.Properties.Training_StudentKey = m_gStudentKey;
                                trnclsinfo.Properties.Catalogue_CourseKey = int.Parse(programinfo.Properties.Catalogue_CourseKey.ToString());
                                trnclsinfo.Properties.Catalogue_LevelKey = int.Parse(programinfo.Properties.Catalogue_LevelKey.ToString());
                                trnclsinfo.Properties.Catalogue_MajorKey = int.Parse(programinfo.Properties.Catalogue_MajorKey.ToString());
                                trnclsinfo.Properties.Catalogue_StatusKey = 1;
                                trnclsinfo.Properties.Catalogue_TrainingTypeKey = int.Parse(programinfo.Properties.Catalogue_TrainingTypeKey.ToString());
                                trnclsinfo.Properties.ID = IDafter;
                                trnclsinfo.Properties.Training_ClassKey = ClassAfter;
                                trnclsinfo.Properties.Training_TrainingProgramKey = m_iProgramAfter;
                                trnclsinfo.Properties.Mainly = true;
                                trnclsinfo.Save();
                            }

                            // Cập nhật trạng thái chương trình cũ
                            trnclsinfo = new Training_Student_ClassInfo(m_gStudentKey.ToString(), m_iProgramBefore, true);
                            trnclsinfo.Properties.Mainly = false;
                            trnclsinfo.Save();
                            // Ghi nhận phê duyệt
                            tranferinfo.Properties.Permit = true;
                            tranferinfo.Properties.Status = true;
                            if (rtbFeedback.Text != "")
                                tranferinfo.Properties.Feedback = rtbFeedback.Text;
                            tranferinfo.Properties.TranferUser = Utils.LoginInfo.GblLoginInfo.sysLoginUserKey;
                            tranferinfo.Properties.CensorshipTime = SysDateTime.Now;
                            tranferinfo.Save();

                            XtraMessageBox.Show("Đã phê duyệt phiếu " + m_iLog_TranferKey, "Thông báo");
                            e.Cancel = true;
                            return;
                        }
                        else
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("Phiếu " + m_iLog_TranferKey + " đã duyệt. Không thể chỉnh sửa");
                        e.Cancel = true;
                        return;
                    }
                }
            }

            if (this.grvView.FocusedColumn == gclRefuse)
            {
                m_iLog_TranferKey = int.Parse(this.grvView.GetRowCellValue(this.grvView.FocusedRowHandle, "Log_TranferKey").ToString());
                Log_TranferInfo tranferinfo = new Log_TranferInfo(m_iLog_TranferKey);
                ShowInformation(m_iLog_TranferKey);
                if (tranferinfo.Properties.Log_TranferKey > 0)
                {
                    GetValues(m_iLog_TranferKey);
                    if (tranferinfo.Properties.Status.Value == false)
                    {
                        if (rtbFeedback.Text != "")
                        {
                            DialogResult result = XtraMessageBox.Show("Bạn không muốn phê duyệt phiếu " + m_iLog_TranferKey, "Thông báo", MessageBoxButtons.YesNo);
                            if (result == DialogResult.Yes)
                            {
                                m_bRefuseValue = (bool)this.grvView.GetFocusedRowCellValue(gclRefuse);
                                this.grvView.SetFocusedRowCellValue(gclRefuse, !m_bRefuseValue);

                                // Ghi nhận phê duyệt
                                if (rtbFeedback.Text != "")
                                    tranferinfo.Properties.Feedback = rtbFeedback.Text;
                                tranferinfo.Properties.Refuse = true;
                                tranferinfo.Properties.Status = true;
                                tranferinfo.Properties.TranferUser = Utils.LoginInfo.GblLoginInfo.sysLoginUserKey;
                                tranferinfo.Properties.CensorshipTime = SysDateTime.Now;
                                tranferinfo.Save();

                                e.Cancel = true;
                                return;
                            }
                            else
                            {
                                e.Cancel = true;
                                return;
                            }
                        }
                        else
                        {
                            Frm_AcademicHandling_Transfer_Course_Censorship_Confirm confirm = new Frm_AcademicHandling_Transfer_Course_Censorship_Confirm(tranferinfo);
                            confirm.ShowDialog();
                            if (Log_TranferData.m_bFlag)
                            {
                                this.grvView.SetFocusedRowCellValue(gclRefuse, true);
                                e.Cancel = true;
                                return;
                            }
                            else
                            {
                                e.Cancel = true;
                                return;
                            }
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("Phiếu " + m_iLog_TranferKey + " đã duyệt. Không thể chỉnh sửa");
                        e.Cancel = true;
                        return;
                    }
                }
            }
        }
    }
}
